
#include "type.h"
#include "list.h"

//----------------------------------------------------add list func
void list_init(list_t *plist)
{
	plist->head.prev = NULL;
	plist->head.next = &plist->tail;
	plist->tail.prev = &plist->head;
	plist->tail.next = NULL;
}
//---------------------------------------------
// next 3 furnction for traversing 
list_node_t *list_begin(list_t *plist)
{
	return plist->head.next;
}

list_node_t *list_next(list_node_t *pnode)
{
	return pnode->next;
}

list_node_t *list_end(list_t *plist)
{
	return &plist->tail;
}
//---------------------------------------------

list_node_t *list_front(list_t *plist)
{
	return plist->head.next;
}

list_node_t *list_back(list_t *plist)
{
	return plist->tail.prev;
}

//---------------------------------------------
void list_insert_before(list_node_t *before, list_node_t *pnode)
{
	pnode->prev = before->prev;
	pnode->next = before;
	before->prev->next = pnode;
	before->prev = pnode;
}

void  list_insert_after(list_node_t *after, list_node_t *pnode)
{
	pnode->prev = after;
	pnode->next = after->next;
	after->next->prev = pnode;
	after->next = pnode;
}

list_node_t *list_delete(list_node_t *pnode)
{
	pnode->prev->next = pnode->next;
	pnode->next->prev = pnode->prev;
	return pnode->next;
}
//---------------------------------------------
// next 2 functin for insert
void list_insert_front(list_t *plist, list_node_t *pnode)
{
	list_insert_before(list_begin(plist), pnode);
}

void list_insert_back(list_t *plist, list_node_t *pnode)
{
	list_insert_before(list_end(plist), pnode);
}

//---------------------------------------------
// next 2 function for delete from list
list_node_t *list_delete_front(list_t *plist)
{
	list_node_t *front = list_front(plist);
	list_delete(front);
	return front;
}

list_node_t *list_delete_back(list_t *plist)
{
	list_node_t *back = list_back(plist);
	list_delete(back);
	return back;
}
u16	list_get_count(list_t* plist)
{
	u16 cnt=0;
	list_node_t* pnode;
	for (pnode = list_begin(plist); pnode != list_end(plist);pnode = list_next(pnode))
	{
		cnt++;
	}
	return cnt;
}
//---------------------------------------------


